# 试验1：字典中是否可以允许存入重复键值对
# 初始化
dictData = {'a': 1, 'b': 2}
# 附加一个完全完全相同的键值对
dictData['a'] = 2
dictData['c'] = 3
print(dictData)
# 删除
del dictData['a']
print(dictData)
dictData.pop('b')
print(dictData)
# 解除字典中的所有内容
dictData.clear()
print(dictData)

############################################
# 试验2：通过random和string对象创建随机字符串
# 导入库文件
import random, string

# 获取数字字符
d = string.digits
print(d)
# 获取小写字母
letter = string.ascii_letters
print(letter)
# 获取符号
pun = string.punctuation
print(pun)
# 获取随机数字
dig = random.choice(d)
print(dig)
# 随机获取字母
le = random.choice(string.ascii_letters)
print(le)
# 随机获取符号
pu = random.choice(string.punctuation)
print(pu)

# 获取多少个随机的字符，用随机函数可以控制
number = random.randint(1, 10)
print(number)
sam = random.sample(d, number)
print(sam)
# 将列表中的元素放入字符串
result = ''.join(sam)
print(result)

#########################################
# 试验3：进行字符串组合
# import random, string
# 获取相关长度随机数
n1 = random.randint(1, len(string.digits))
n2 = random.randint(1, len(string.ascii_letters))
n3 = random.randint(1, len(string.punctuation))
print(n1, n2, n3)

# 获取随机长度的指定字符
con1 = random.sample(string.digits, n1)
con2 = random.sample(string.ascii_letters, n2)
con3 = random.sample(string.punctuation, n3)
print(con1, con2, con3)

# 进行相关内容的合并
r1 = ''.join(con1)
r2 = ''.join(con2)
r3 = ''.join(con3)
res = r1 + r2 + r3
print(res)

########################################
# 试验4：重复的随机字符
# 字符串类型转化为list类型
listr = list(res)
# print(listr)
number = len(listr)
for i in range(number):
    n = random.randint(1, number)
    m = random.randint(0, number)
    # print(f"位置为--{n}")
    # 取出随机内容
    tmp = listr[m]
    # print(f"取出内容为--{tmp}")
    listr.insert(n, tmp)
# 通过join方法连接字符串
result = ''.join(listr)
print(result)

###################################
# 试验5：由用户控制随机字符串长度
userNumber = int(input("请输入需要的字符串长度"))
# 进行长度的分配
mother_len = userNumber // 3
child_len = userNumber - mother_len
print("母串长度:", mother_len)
print("子串长度:", child_len)

n1 = random.randint(1, len(string.digits))
n2 = random.randint(1, len(string.ascii_letters))
n3 = random.randint(1, len(string.punctuation))
print(n1, n2, n3)

# 获取随机长度的指定字符
con1 = random.sample(string.digits, n1)
con2 = random.sample(string.ascii_letters, n2)
con3 = random.sample(string.punctuation, n3)
print(con1, con2, con3)

# 进行相关内容的合并
r1 = ''.join(con1)
r2 = ''.join(con2)
r3 = ''.join(con3)
res = r1 + r2 + r3
print(res)

ori_mother_len = len(res)
print("原始母串长度", ori_mother_len)

# 如果原始母串长度大于规定长度，则进行随机数截取
if ori_mother_len > mother_len:
    r = random.sample(res, mother_len)
    r = ''.join(r)
    print("最终字符长度及内容", len(r), r)
elif ori_mother_len < mother_len:
    tmplist = []
    for i in range(mother_len - ori_mother_len):

        tmplist.append(random.choice(res))
    print("补充字符串长度：", len(tmplist))
    r = res + ''.join(tmplist)
    print("最终字符串长度：", len(r))
